<template>
  <el-row>
    <el-card>
      <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item :label="$t('letterReviewMember.searchModel.company')">
          <el-select v-model="searchForm.firmId" clearable :placeholder="$t('letterReviewMember.prompt.company')">
            <el-option
              v-for="item in allFirm"
              :key="item.id"
              :label="item['firmName']"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('letterReviewMember.searchModel.companyType')">
          <el-select v-model="searchForm.firmType" clearable :placeholder="$t('letterReviewMember.prompt.companyType')">
            <el-option
              v-for="item in $t('letterReviewMember.companyType1')"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('letterReviewMember.searchModel.group')">
          <el-select v-model="searchForm.groupId" :placeholder="$t('letterReviewMember.prompt.group')">
            <el-option-group
              v-for="group in groupSelect"
              :key="group.label"
              :label="group.label"
            >
              <el-option
                v-for="item in group.options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-option-group>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('letterReviewMember.personnel.stage')">
          <el-select v-model="searchForm.stageId" :placeholder="$t('letterReviewMember.prompt.stage')" style="width: 300px">
            <el-option
              v-for="item in allStage"
              :key="item.id"
              :label="item.flowName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('letterReviewMember.searchModel.status')">
          <el-select v-model="searchForm.status" :placeholder="$t('letterReviewMember.prompt.status')">
            <el-option
              v-for="item in $t('letterReviewMember.accountType1')"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('letterReviewMember.searchModel.personnelName')">
          <el-input v-model="searchForm.staffName" clearable :placeholder="$t('letterReviewMember.prompt.personnelName')" :maxlength="50" />
        </el-form-item>
        <el-form-item :label="$t('letterReviewMember.searchModel.phoneNumber')">
          <el-input v-model="searchForm.phonenumber" clearable :placeholder="$t('letterReviewMember.prompt.phoneNumber')" :maxlength="50" />
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="search">
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="reset">
            {{ $t('common.reset') }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <el-button type="primary" @click="add">
        {{ $t('letterReviewMember.personnelAdd') }}
      </el-button>
      <el-table
        ref="multipleTable"
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        :stripe="true"
        style="width: 100%"
      >
        <el-table-column
          fixed="left"
          prop="id"
          :label="tableText['id']"
          min-width="80"
        />
        <el-table-column
          fixed="left"
          prop="staffName"
          :label="tableText['name']"
          min-width="150"
        />
        <el-table-column
          prop="phonenumber"
          :label="tableText['phoneNumber']"
          min-width="150"
        />
        <el-table-column
          prop="groupId"
          :label="tableText['group']"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ setGroup(scope.row['groupId']) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="stageId"
          :label="tableText['stage']"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ setStage(scope.row['stageId']) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="firmName"
          :label="tableText['company']"
          min-width="150"
        />
        <el-table-column
          prop="firmType"
          :label="tableText['companyType']"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ setFirmType(scope.row['firmType']) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="authority"
          :label="tableText['permission']"
          min-width="300"
        >
          <template slot-scope="scope">
            {{ setAuthority(scope.row['authority']) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="status"
          :label="tableText['status']"
          min-width="100"
        >
          <template slot-scope="scope">
            {{ setStatus(scope.row['status']) }}
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          :label="$t('common.operate.name')"
          min-width="100"
        >
          <template slot-scope="scope">
            <el-button size="small" @click="editItem(scope.row)">{{ $t('common.operate.edit') }}</el-button>
          </template>
        </el-table-column>

      </el-table>
      <div class="block">
        <el-pagination
          :current-page="searchForm.currentPage"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="searchForm.pageSize"
          :total="totalNumber"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
      <personnel-dialog
        :show-dialog="editDialog"
        :group-select="groupSelect"
        :choose-item="chooseItem"
        @change-dialog="changeDialog"
        @edit-success="editSuccess"
      />
    </el-card>
  </el-row>
</template>

<script>
import { queryCreditReviewStaff } from '@/api/risk'
import PersonnelDialog from '@/components/risk/riskManagement/personnel-dialog'
export default {
  name: 'PersonnelManagement',
  components: {
    PersonnelDialog
  },
  props: {
    allFirm: {
      type: Array,
      default: function() {
        return []
      }
    },
    allGroup: {
      type: Array,
      default: function() {
        return []
      }
    },
    allStage: {
      type: Array,
      default: function() {
        return []
      }
    },
    groupSelect: {
      type: Array,
      default: function() {
        return []
      }
    }
  },
  data() {
    return {
      loading: true,
      editDialog: false,
      tableText: {},
      totalNumber: 0,
      tableData: [],
      searchForm: this.params(),
      chooseItem: {
        id: null,
        staffName: null,
        phonenumber: null,
        groupId: null,
        authority: null,
        username: null,
        password: null,
        recallPhone: null,
        recallPhonePassword: null,
        status: null,
        releaseCase: null
      }
    }
  },
  mounted() {
    this.tableText = this.$t('letterReviewMember.personnel')
    this.getData()
  },
  methods: {
    params() {
      return {
        currentPage: 1,
        pageSize: 10,
        firmId: '',
        firmType: '',
        groupId: null,
        stageId: '',
        status: '',
        staffName: null,
        phonenumber: null
      }
    },
    search() {
      this.searchForm.currentPage = 1
      this.getData()
    },
    reset() {
      this.searchForm = this.params()
      this.getData()
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.searchForm.currentPage = val
      this.getData()
    },
    getData() {
      this.loading = true
      queryCreditReviewStaff(this.searchForm)
        .then(res => {
          this.loading = false
          if (res.success) {
            this.tableData = res.data
            this.totalNumber = res.page ? res.page['totalNumber'] : 0
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    add() {
      this.choose({})
      this.editDialog = true
    },
    choose(param) {
      this.chooseItem.id = param.id
      this.chooseItem.staffName = param.staffName
      this.chooseItem.phonenumber = param.phonenumber
      this.chooseItem.groupId = param.groupId
      this.chooseItem.authority = param.authority ? param.authority.split('_') : null
      this.chooseItem.username = param.username
      this.chooseItem.password = param.password
      this.chooseItem.recallPhone = param.recallPhone
      this.chooseItem.recallPhonePassword = param.recallPhonePassword
      this.chooseItem.status = param.status
      this.chooseItem.releaseCase = param.releaseCase
    },
    editItem(item) {
      this.choose(item)
      this.editDialog = true
    },
    changeDialog(param) {
      this.editDialog = param
      setTimeout(() => {
        this.choose({})
      }, 10)
    },
    editSuccess() {
      this.getData()
    },
    setGroup(groupId) {
      if (groupId !== null) {
        const name = this.allGroup.filter(item => {
          return item.id === groupId
        })
        return (name.length > 0 && name[0].groupName) ? name[0].groupName : ''
      }
    },
    setStage(stageId) {
      if (stageId !== null) {
        const name = this.allStage.filter(item => {
          return item.id === stageId
        })
        return (name.length > 0 && name[0].flowName) ? name[0].flowName : ''
      }
    },
    setFirmType(firmType) {
      if (firmType !== null) {
        const name = this.$t('letterReviewMember.companyType').filter(item => {
          return item.value === firmType
        })
        return (name.length > 0 && name[0].desc) ? name[0].desc : ''
      }
    },
    setAuthority(authority) {
      if (authority !== null) {
        const arr = []
        authority.split('_').forEach(v => {
          const name = this.$t('letterReviewMember.permissions').filter(item => {
            return item.value === v
          })
          if (name.length > 0 && name[0].desc) {
            arr.push(name[0].desc)
          }
        })
        return arr.length > 0 ? arr.join(', ') : ''
      }
    },
    setStatus(status) {
      if (status !== null) {
        const name = this.$t('letterReviewMember.status').filter(item => {
          return item.value === status
        })
        return (name.length > 0 && name[0].desc) ? name[0].desc : ''
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card{
  margin-bottom: 10px;
}
.el-table{
  margin-top: 10px;
}
</style>
